package WiFi;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import client.ClientConnectable;

public class WiFiConnectToThread extends Thread {

	private static final String WI_FI_CONNECT_TO_THREAD = "WiFiConnectToThread";
	
	private static final int PORT_TARGET = 2222;
	private String ip;

	private final ClientConnectable client;

	public WiFiConnectToThread(String ip, ClientConnectable client) {
		super(WI_FI_CONNECT_TO_THREAD);
		this.ip = ip;
		this.client = client;
	}

	@Override
	public void run(){
		InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, PORT_TARGET);
		Socket socket = new Socket();
		try {
			System.out.println("! Tentando conectar em " + inetSocketAddress.getHostName());
			socket.connect(inetSocketAddress,9999);
			WiFiConnectedThread connectedThread = new WiFiConnectedThread(socket, client);
			System.out.println("! conectado em " + inetSocketAddress.getHostName());
			client.setConnectedThread(connectedThread);
		} catch (IOException e) {
			System.err.println("! Erro ao tentar conectar WiFi no ip ->  " + ip);
			//e.printStackTrace();
		}catch (Exception e) {
			
		}
	}

	

}
